#!/bin/bash

# 1. CentOS 7.7
# 2. Docker 19.03.5
# 3. Kubeadm 1.17.1

function config_env {
    # 关闭防火墙
    systemctl stop firewalld && systemctl disable firewalld
    systemctl stop iptables && systemctl disable iptables
    iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
  
    # 关闭selinux
    setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

    # 关闭swap
    swapoff -a && sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

    # 设置系统参数
    echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
    echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
    echo 'net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1' > /etc/sysctl.d/k8s.conf
    sysctl --system
}

function install_docker {
    yum install -y docker-ce-19.03.5 docker-ce-cli-19.03.5 containerd.io
    systemctl enable docker && systemctl start docker
}

function install_k8s {
    yum install -y kubeadm-1.17.1 kubelet-1.17.1 kubectl-1.17.1
  
    systemctl enable kubelet && systemctl start kubelet
}

function create_cluster {
    kubeadm init --kubernetes-version=v1.17.1 --pod-network-cidr=10.244.0.0/16
  
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
  
}

function main {
    config_env            # 环境配置
  
    install_k8s           # 安装kubeadm kubectl kubelet
  
    import_images         # 导入Docker镜像包
  
    create_cluster        # kubeadm init
    
    deploy_network        # 部署网络插件
  
    deploy_dashboard      # 部署dashboard
}

main